#ifndef __GKOS_THREAD_H
#define __GKOS_THREAD_H

#include "lib/global.h"
typedef void thread_func(void *);

enum task_status {
    TASK_RUNNING,
    TASK_READY,
    TASK_BLOCKED,
    TASK_WAITING,
    TASK_HANGING,
    TASK_DIED
};

/**
 * @brief 中断保护时的上下文
 */
struct intr_frame {
    uint32_t vec_no;
    uint32_t edi;
    uint32_t esi;
    uint32_t ebp;
    uint32_t esp_dummy;
    uint32_t ebx;
    uint32_t edx;
    uint32_t ecx;
    uint32_t eax;
    uint32_t gs;
    uint32_t fs;
    uint32_t es;
    uint32_t ds;

    uint32_t err_code;
    void (*eip) (void);
    uint32_t cs;
    uint32_t eflags;
    void *esp;
    uint32_t ss;
};

struct thread_frame {
    uint32_t ebp;
    uint32_t ebx;
    uint32_t edi;
    uint32_t esi;

    void (*eip) (thread_func * func, void * func_arg);

    // 以下当第一次调度时使用
    void (*unused_retaddr);
    thread_func * func;
    void * func_arg;
};

struct task_pcb
{
    uint32_t* self_kstack;
    enum task_status status;
    uint8_t priority;
    char name[16];
    uint32_t stack_magic;       //栈边界
};

void thread_create(struct task_pcb* pthread, thread_func func, void* func_arg);

void init_thread(struct task_pcb* pthread, char* thread_name, int prio);

struct task_pcb* thread_start(char* thread_name, int prio, thread_func func, void* func_arg);

#endif